﻿#region License
/*
The MIT License

Copyright (c) 2008 Sky Morey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion
using System;
using Native_.DirectShow;
using System.Diagnostics;
using System.ComponentModel;
namespace Capture.DirectShow
{
    /// <summary>
    /// IFrameInfo
    /// </summary>
    public interface IFrameInfo
    {
        void NewFrame(double sampleTime, IntPtr buffer, int bufferLen);
        void SetProperties(VideoInfoHeader header);
    }

    /// <summary>
    /// FrameInfo
    /// </summary>
    public class FrameInfo : IFrameInfo
    {
        private int _height;
        private int _width;
        // framerate
        private Stopwatch _frameTimer = Stopwatch.StartNew();
        private double _frames;

        public virtual void NewFrame(double sampleTime, IntPtr buffer, int bufferLen)
        {
            UpdateFramerate();
        }

        public virtual void SetProperties(VideoInfoHeader header)
        {
            Width = header.BmiHeader.Width;
            Height = header.BmiHeader.Height;
        }

        public int Width
        {
            get { return _width; }
            private set
            {
                _width = value;
                OnPropertyChanged("Width");
            }
        }

        public int Height
        {
            get { return _height; }
            private set
            {
                _height = value;
                OnPropertyChanged("Height");
            }
        }

        public float Framerate { get; set; }

        private void UpdateFramerate()
        {
            _frames++;
            if (_frameTimer.ElapsedMilliseconds >= 1000)
            {
                Framerate = (float)Math.Round(_frames * 1000 / _frameTimer.ElapsedMilliseconds);
                _frameTimer.Reset();
                _frameTimer.Start();
                _frames = 0;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
